package cn.agiledata.bank.wap.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.account.service.AccountSummary;
import cn.agiledata.bank.common.exception.BusinessException;
import cn.agiledata.bank.common.service.ICommonService;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.SecurityUtil;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.UniqueIdentityUtil;
import cn.agiledata.bank.hibernateBean.Account;
import cn.agiledata.bank.hibernateBean.SubAccount;
import com.vnv.infrastructure.exception.IPBException;


public class LoginAction extends WapBaseAction implements Constant{
	/**
	 * wap 登陆
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward login(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws IPBException {
		String loginName = "";
		String loginPassword = "";
		try {
			loginName = (String)PropertyUtils.getSimpleProperty(form, "loginName");
			loginPassword = (String)PropertyUtils.getSimpleProperty(form, "loginPassword");
		} catch (Exception e) {
			return messageProcess(INFRA_FAILURE,HOME,form,mapping,request);
		}
		if(StringUtil.isStrEmpty(loginName)) { //手机号为空
			return messageProcess(LOGINNAME_EMPTY,HOME,form,mapping,request);
		}
		if(StringUtil.isStrEmpty(loginPassword)) { //登陆密码为空
			return messageProcess(LOGIN_PASSWORD_EMPTY,HOME,form,mapping,request);
		}
		if(loginPassword.length()<6 || loginPassword.length()>15) {
			return messageProcess(LOGIN_PASSWORD_LENGTH_ERROR,HOME,form,mapping,request);
		}
		
		if(!StringUtil.isMobile(loginName)) { //手机号码格式错误
			return messageProcess(ERROR_MOBILE,HOME,form,mapping,request);
		}
		loginPassword = SecurityUtil.encryptoMD5String(loginPassword);
		ICommonService service = getCommonService();
		AccountSummary accountSummary = null;
		Map map = new HashMap();
		map.put("username",loginName);
		map.put("password",loginPassword);
		try {
			accountSummary = service.userLogon(Constant.WAP,map);
		} catch (BusinessException e) {
			return messageProcess(e.getReturnCode(),HOME,form,mapping,request);
		}
		SubAccount subAccount = service.getWapSubAccount(accountSummary);
		if(subAccount != null) {
			request.setAttribute("subAccountId",subAccount.getId().toString());
			request.setAttribute("accountId",String.valueOf(accountSummary.getAccountId()));
			request.setAttribute("subAccountBalance",subAccount.getAccountBalance().getBalance().toString());
			return mapping.findForward(MAIN);
		} else {
			return messageProcess(LOGON_ACCOUNT_NOT_EXIST,HOME,form,mapping,request);
		}
		
	}
	
	/**
	 * wap 注册
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward register(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws IPBException, Exception {
		String loginName = request.getParameter("loginName");
		String userName = request.getParameter("userName");
		String loginPassword = request.getParameter("loginPassword");
		String payPassword = request.getParameter("payPassword");
		if(StringUtil.isStrEmpty(loginName)) { //手机号为空
			return messageProcess(LOGINNAME_EMPTY,HOME,form,mapping,request);
		}
		if(StringUtil.isStrEmpty(loginPassword)) { //登陆密码为空
			return messageProcess(LOGIN_PASSWORD_EMPTY,HOME,form,mapping,request);
		}
		if(StringUtil.isStrEmpty(payPassword)) { //支付密码为空
			return messageProcess(PAY_PASSWORD_EMPTY,HOME,form,mapping,request);
		}
		if(StringUtil.isStrEmpty(userName)) { //姓名为空
			return messageProcess(USERNAME_EMPTY,HOME,form,mapping,request);
		}
		if(loginPassword.length()<6 || loginPassword.length()>15) {
			return messageProcess(LOGIN_PASSWORD_LENGTH_ERROR,HOME,form,mapping,request);
		}
		if(payPassword.length()<6 || payPassword.length()>15) {
			return messageProcess(PAY_PASSWORD_LENGTH_ERROR,HOME,form,mapping,request);
		}		
		if(!StringUtil.isMobile(loginName)) { //手机号码格式错误
			return messageProcess(ERROR_MOBILE,HOME,form,mapping,request);
		}
		ICommonService service = getCommonService();
		if(service.findAccountBySign(loginName) != null) { //手机已经注册过了
			return messageProcess(REGISTER_MOBILE_DUPLICATED,HOME,form,mapping,request);
		}
		Map map = new HashMap();
		map.put("mobile",loginName);
		map.put("type",Constant.ACCOUNT_TYPE_BUYER);  //个人客户
		map.put("loginPassword",loginPassword);  //登陆密码
		map.put("payPassword",payPassword);  //支付密码
		map.put("name",userName);  //个人姓名
		service.userRegister(Constant.WAP,Constant.PROJECT_WEBPAY,map); 
		return messageProcess(REGISTER_SUCCESS,HOME,form,mapping,request);
	}
	
	/**
	 * 修改登陆密码
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward loginPasswordMofify(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws IPBException, Exception {
		String loginPassword = (String)PropertyUtils.getSimpleProperty(form, "loginPassword");
		String confirmLoginPassword = (String)PropertyUtils.getSimpleProperty(form, "confirmLoginPassword");
		if(StringUtil.isStrEmpty(loginPassword)) { //登陆密码为空
			return messageProcess(LOGIN_PASSWORD_EMPTY,MAIN,form,mapping,request);
		}
		if(StringUtil.isStrEmpty(confirmLoginPassword)) { //确认登陆密码为空
			return messageProcess(CONFIRM_LOGIN_PASSWORD_EMPTY,MAIN,form,mapping,request);
		}
		if(!loginPassword.equals(confirmLoginPassword)) { //登陆密码和确认登陆密码不匹配
			return messageProcess(LOGIN_PASSWORD_AND_CONFIRM_LOGIN_PASSWORD_NOT_MATCH,MAIN,form,mapping,request);
		}
		if(confirmLoginPassword.length()<6 || confirmLoginPassword.length()>15) {
			return messageProcess(LOGIN_PASSWORD_LENGTH_ERROR,MAIN,form,mapping,request);
		}
		String accountId = (String)PropertyUtils.getSimpleProperty(form, "accountId");
		ICommonService service = getCommonService();
		Account account = service.getAccount(accountId);
		loginPassword = SecurityUtil.encryptoMD5String(loginPassword);
		account.setLoginPassword(loginPassword);
		service.updateAccount(account);
		return messageProcess(MODIFY_LOGIN_PASSWORD_SUCCESS,MAIN,form,mapping,request);
	}
	/**
	 * 修改支付密码
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward payPasswordMofify(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws IPBException, Exception {
		String payPassword = (String)PropertyUtils.getSimpleProperty(form, "payPassword");
		String confirmPayPassword = (String)PropertyUtils.getSimpleProperty(form, "confirmPayPassword");
		if(StringUtil.isStrEmpty(payPassword)) { //登陆密码为空
			return messageProcess(PAY_PASSWORD_EMPTY,MAIN,form,mapping,request);
		}
		if(StringUtil.isStrEmpty(confirmPayPassword)) { //确认登陆密码为空
			return messageProcess(CONFIRM_PAY_PASSWORD_EMPTY,MAIN,form,mapping,request);
		}
		if(!payPassword.equals(confirmPayPassword)) { //登陆密码和确认登陆密码不匹配
			return messageProcess(PAY_PASSWORD_AND_CONFIRM_PAY_PASSWORD_NOT_MATCH,MAIN,form,mapping,request);
		}
		if(confirmPayPassword.length()<6 || confirmPayPassword.length()>15) {
			return messageProcess(PAY_PASSWORD_LENGTH_ERROR,MAIN,form,mapping,request);
		}
		String accountId = (String)PropertyUtils.getSimpleProperty(form, "accountId");
		ICommonService service = getCommonService();
		Account account = service.getAccount(accountId);
		payPassword = SecurityUtil.encryptoMD5String(payPassword);
		account.setPayPassword(payPassword);
		service.updateAccount(account);
		return messageProcess(MODIFY_PAY_PASSWORD_SUCCESS,MAIN,form,mapping,request);
	}
	
	/**
	 * 找回登陆密码
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward loginPasswordReturn(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response)throws IPBException, Exception {

		String loginName = request.getParameter("loginName");
		if(StringUtil.isStrEmpty(loginName)) { //手机号为空
			return messageProcess(LOGINNAME_EMPTY,HOME,form,mapping,request);
		}
		if(!StringUtil.isMobile(loginName)) { //手机号码格式错误
			return messageProcess(ERROR_MOBILE,HOME,form,mapping,request);
		}
		ICommonService service = getCommonService();
		Account account = service.findAccountBySign(loginName);
		if(account == null) {
			return messageProcess(LOGON_ACCOUNT_NOT_EXIST,HOME,form,mapping,request);
		}
		Map map = new HashMap();
		map.put("loginPassword",UniqueIdentityUtil.createRandomStringAndNumber(4));
		
		service.returnLoginPasswordBySMS(loginName,map);
		return messageProcess(RETURN_LOGIN_PASSWORD_SUCCESS,HOME,form,mapping,request);
	}
}
